package me.zhujin.java.learnjava.myserver;

import me.zhujin.java.learnjava.myserver.servlet.HTMLServlet;
import me.zhujin.java.learnjava.myserver.servlet.ScriptMspServlet;
import me.zhujin.java.learnjava.myserver.webxml.MappingXml;
import me.zhujin.java.learnjava.myserver.webxml.ServletXml;
import me.zhujin.java.learnjava.myserver.webxml.WebHandler;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by zhujin on 10/24/16.
 * blog:www.zhujin.me
 * email: zhujin.nova@gmail.com
 */
public class WebApp {
    private static MyServletContext context;

    static {
//        context = new MyServletContext();
//        Map<String, String> mapping = context.getMapping();
//        mapping.put("/login", "login");
//        mapping.put("/log", "login");
//        mapping.put("/reg", "register");
//        Map<String, String> servlet = context.getServlet();
//        servlet.put("login", "me.zhujin.java.learnjava.myserver.servlet.LoginMyServlet");
//        servlet.put("register", "me.zhujin.java.learnjava.myserver.servlet.RegisterMyServlet");


        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser sax = factory.newSAXParser();
            WebHandler webHandler = new WebHandler();

            sax.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream(Const.WEB_XML), webHandler);
            context = new MyServletContext();
            Map<String, String> mapping = context.getMapping();
            for (MappingXml mappingXml : webHandler.getMappingXmlList()) {
                for (String url_pattern : mappingXml.getUrlPattern()) {
                    mapping.put(url_pattern, mappingXml.getServletName());
                }
            }
            Map<String, String> servlet = context.getServlet();
            for (ServletXml servletXml : webHandler.getServletXmlList()) {
                servlet.put(servletXml.getServletName(), servletXml.getClz());
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
//        //me.zhujin.java.learnjava.myserver.servlet
////        Class<?> clz = Class.forName("me.zhujin.java.learnjava.myserver.servlet.LoginMyServlet");
////        MyServlet servlet = (MyServlet) clz.newInstance();
//
//    }

    public static MyServlet getServlet(String url) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        if (url == null || url.trim().isEmpty()) {
            return null;
        }
        if (url.endsWith(".htm")||url.endsWith(".html")){
            return new HTMLServlet();
        }
        Map<String, String> mapping = context.getMapping();
        if (!mapping.containsKey(url)) {
            if (url.endsWith(".msp")||url.endsWith(".jsp")){
                return new ScriptMspServlet();
            }
            System.out.println(Arrays.toString(mapping.keySet().toArray()));
            System.out.println(url);
            return null;
        }
        return (MyServlet) Class.forName(context.getServlet().get(context.getMapping().get(url))).newInstance();
    }

}
